package test.mina.flight;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class Client {
	
	public static void main(String[] args) throws UnknownHostException, IOException {
		IoConnector connector = new NioSocketConnector();
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new FlightClientCodecFactory()));
		connector.setHandler(new IoHandlerAdapter());
		ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 6565));
		future.addListener(new IoFutureListener<IoFuture>() {
			public void operationComplete(IoFuture future) {
				IoSession session = future.getSession();
//				public String startCity;
//				public String endCity;
//				public String flightway;
//				public String date;
//				public String fromDate;
//				public String subclass1;
//				public String flight1;
				Flight flight = new Flight();
				flight.setStartCity("beijing");
				flight.setEndCity("shanghai");
				flight.setDate("2015-06-08");
				flight.setFlightway("1");
				session.write(flight);
			};
		});
	}

}
